package com.parkinglot.util;

import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Component
public class DateUtils {
    private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取当前时间字符串
     */
    public static String getNow() {
        return sf.format(new Date());
    }

    /**
     * 日期转字符串
     */
    public static String getFormatDate(Date date) {
        return sf.format(date);
    }

    /**
     * 字符串转日期
     */
    public static Date getParse(String date) throws ParseException {
        return sf.parse(date);
    }

    /**
     * 计算小时时间差
     */
    public static int sjc(String firsttime, String endtime) throws ParseException {
        Date fromDate1 = getParse(firsttime);
        Date toDate1 = getParse(endtime);
        long from1 = fromDate1.getTime();
        long to1 = toDate1.getTime();
        int minutes = (int) ((to1 - from1) / (1000 * 60));
        return minutes;
    }

    /**
     * 日期加天数
     *
     * @param
     * @throws ParseException
     */
    public static String plusDay2(int num, String date) throws ParseException {
        Date d = getParse(date);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        d = ca.getTime();
        String enddate = sf.format(d);
        System.out.println("增加天数以后的日期：" + enddate);
        return enddate;
    }
    public static  String dateadd(String date,int num) throws ParseException {
        Date d = getParse(date);
        Calendar   calendar = new GregorianCalendar();
        calendar.setTime(d);
        calendar.add(calendar.DATE,num); //把日期往后增加一天,整数  往后推,负数往前移动
        Date dates=calendar.getTime();
        return getFormatDate(dates);
    }


    /**
     * 取得当前时间戳（精确到秒）
     * @return
     */
    public static String timeStamp(long time){

        String t = String.valueOf(time/1000);
        return t;
    }
    public static void main(String[] args) throws ParseException {
       String firsttime= "2020-12-10 08:15:59";
       String end="2020-12-10 11:40:59";
     int s=  sjc(firsttime,end);
        System.out.println(s);
     int a=s%60;
     int b=s/60;
        System.out.println(a);
        System.out.println(b);
        BigDecimal big= BigDecimal.valueOf(s);
        System.out.println(big);
    }
}
